package com.jiuyin.dianjing.ui.activity.community;

import android.view.View;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.ReplyAdapter;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.interfaces.ProgressDialogCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.CommentModel;
import com.jiuyin.dianjing.model.ThumbModel;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.DisplayUtil;
import com.jiuyin.dianjing.util.IMEUtil;
import com.jiuyin.dianjing.util.LogUtil;
import com.jiuyin.dianjing.util.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMMENT_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_CONTENT;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TARGETID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;

public class CommentActivity extends BaseActivity {
    public static final String MODEL = "model";
    CommentModel commentModel;
    @BindView(R.id.rv_comment)
    RecyclerView rvReply;
    @BindView(R.id.iv_send)
    View ivSend;
    @BindView(R.id.et_comment)
    EditText etReply;
    @BindView(R.id.tv_favorite)
    CheckedTextView tvFavorite;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.tv_reply)
    TextView tvReply;

    private ReplyAdapter replyAdapter;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_comment;
    }

    @Override
    protected void initData() {
        commentModel = (CommentModel) getIntent().getSerializableExtra(MODEL);
    }

    @Override
    protected void initView() {
        setTitle("全部评论");
        replyAdapter = new ReplyAdapter(commentModel.replyList);
        rvReply.setAdapter(replyAdapter);
        Glide.with(this)
                .load(commentModel.header)
                .override(DisplayUtil.dp2px(this, 50))
                .into(ivHead);
        tvName.setText(commentModel.nickname);
        tvTime.setText(commentModel.create_time);
        tvReply.setText(commentModel.reply_num + "");
        tvComment.setText(commentModel.content);
        ivSend.setOnClickListener(v -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put(KEY_TOKEN, HelperApplication.getToken());
            map.put(KEY_CONTENT, etReply.getText().toString());
            map.put(KEY_TARGETID, commentModel.clientuser_id);
            map.put(KEY_COMMENT_ID, commentModel.topiccomment_id);
            JSONObject json = new JSONObject(map);
            ApiResultCallback resultCallback = new ApiResultCallback() {
                @Override
                public void onSuccess(JsonObject data) {
                    CommentModel.Reply replyModel = new Gson().fromJson(data.getAsJsonObject("reply"), CommentModel.Reply.class);
                    commentModel.replyList.add(0, replyModel);
                    replyAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFail(String msg) {
                    LogUtil.log(msg);
                }

                @Override
                public void onError(String msg) {
                    LogUtil.log(msg);
                }
            };
            ServerApi.post(ApiEnum.APP_REPLY_TO_COMMENT_ON_TOPIC.getUrl(), json, null, null, d -> addDisposable(d), resultCallback);
            etReply.setText("");
        });
        tvFavorite.setText(commentModel.thumb_num + "");
        tvFavorite.setChecked(commentModel.isThumb == 1);
        tvFavorite.setOnClickListener(v -> {
            ApiResultCallback apiResultCallback = new ApiResultCallback() {
                @Override
                public void onSuccess(JsonObject data) {
                    ThumbModel thumbModel = new Gson().fromJson(data, ThumbModel.class);
                    tvFavorite.setChecked(thumbModel.isThumb == 1);
                    tvFavorite.setText(thumbModel.thumb_num);
                    tvFavorite.setEnabled(true);
                }

                @Override
                public void onFail(String msg) {
                    ToastUtil.showShort(msg);
                    tvFavorite.setEnabled(true);
                }

                @Override
                public void onError(String msg) {
                    tvFavorite.setEnabled(true);
                    LogUtil.log(msg);
                }
            };
            HashMap<String, Object> map = new HashMap<>();
            map.put(KEY_TOKEN, HelperApplication.getToken());
            map.put(KEY_TARGETID, commentModel.topiccomment_id);//评论或者 帖子主键
            map.put(KEY_TYPE, tvFavorite.isChecked() ? 0 : 1);//0 取消点赞 1 点赞
            JSONObject json = new JSONObject(map);
            tvFavorite.setEnabled(false);
            ServerApi.post(ApiEnum.APP_TOPIC_THUMB.getUrl(), json, null, new ProgressDialogCallback() {
                @Override
                public void showProgressDialog() {
                    showLoading();
                }

                @Override
                public void dismissProgressDialog() {
                    dismissLoading();
                }
            }, d -> addDisposable(d), apiResultCallback);
        });
    }

    @Override
    public void onBackPressed() {
        IMEUtil.hideInputMethod(this);
        setResult(RESULT_OK);
        super.onBackPressed();
    }
}
